package com.jinlejia.erp.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemContractBinding;
import com.jinlejia.erp.ui.common.CommonDeleteDialog;
import com.jinlejia.erp.ui.common.SheetSelect;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
 * 联系方式Adapter
 */
public class ContractAdapter extends YfListAdapter<Map<String, Object>> {

    private Context context;
    ArrayList<Map<String, Object>> typeData;

    public ContractAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
        typeData = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("valueName", "手机");
        map1.put("typeCode", 1);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("valueName", "办公");
        map2.put("typeCode", 2);
        Map<String, Object> map3 = new HashMap<>();
        map3.put("valueName", "住宅");
        map3.put("typeCode", 3);

        typeData.add(map1);
        typeData.add(map2);
        typeData.add(map3);
        if (mData.size() > 2) {
            mData.subList(0, 2);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contract, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = new View(parent.getContext());
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(final RecyclerView.ViewHolder viewHolder, final int i) {
        final ItemContractBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        final HashMap<String, Object> map = (HashMap<String, Object>) mData.get(i);
        if (map.containsKey("id")) {
            //编辑
            if (map.get("contactType").toString().equals("1")) {
                binding.contractType.setText("手机:");
                binding.phone.setText("手机:");
            } else if (map.get("contactType").toString().equals("2")) {
                binding.contractType.setText("办公:");
                binding.phone.setText("办公:");
            } else if (map.get("contactType").toString().equals("3")) {
                binding.contractType.setText("住宅:");
                binding.phone.setText("住宅:");
            }
            mData.get(i).put("typeCode", map.get("contactType").toString());
            mData.get(i).put("number", map.get("tel").toString());
            binding.phoneNumber.setText(map.get("tel").toString());
            binding.phoneNumber.setVisibility(View.VISIBLE);
            binding.contractNumber.setVisibility(View.GONE);
            binding.selectType.setVisibility(View.GONE);
            binding.phone.setVisibility(View.VISIBLE);
            binding.contractType.setVisibility(View.INVISIBLE);

        } else {
            binding.phoneNumber.setVisibility(View.GONE);
            binding.contractNumber.setVisibility(View.VISIBLE);
            binding.contractType.setText(map.get("typeName").toString());
            binding.contractNumber.setHint("请输入" + map.get("typeName").toString());
            if(map.containsKey("number")){
                binding.contractNumber.setText(map.get("number").toString());
            }else{
                binding.contractNumber.setText("");
            }
            binding.contractNumber.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    mData.get(i).put("number", s.toString());
                }
            });
            binding.selectType.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final SheetSelect sheetSelect = new SheetSelect(context, "联系方式", typeData);
                    sheetSelect.show();
                    sheetSelect.setOnSelectListener(new SheetSelect.OnSelectListener() {
                        @Override
                        public void select(Object o) {
                            final Map<String, Object> map = (Map<String, Object>) o;
                            mData.get(i).put("typeName", map.get("valueName"));
                            mData.get(i).put("typeCode", map.get("typeCode"));
                            binding.contractType.setText(map.get("valueName").toString());
                            binding.contractNumber.setHint("请输入".concat(map.get("valueName").toString()).concat("号码"));
                            sheetSelect.dismiss();
                        }
                    });
                }
            });

            binding.contractType.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CommonDeleteDialog deleteDialog = new CommonDeleteDialog(context, "删除联系方式", binding.contractType.getText().toString() + binding.contractNumber.getText());
                    deleteDialog.show();
                    deleteDialog.setConfirmListener(new CommonDeleteDialog.OnConfirmListener() {
                        @Override
                        public void confirm() {
                            mData.remove(i);
                            notifyDataSetChanged();
                        }
                    });
                }
            });
        }


        binding.getRoot().setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }


    static final class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }


}
